#include <stdio.h>
#include <sys/unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <sys/mman.h>
int Open(const char *filename,int flags,mode_t mode)
{
    int fd=open(filename,flags,mode);
    if(fd==-1)
        {
            printf("open file error:%s",strerror(errno));
            return -1;
        }

    return fd;
}
int Close(int fd)
{
    int c=close(fd);
    if(c==-1)
        {
            printf("close file error:%s",strerror(errno));
            return -1;
        }
    return c;
}
int Write(int fd,void*buf,size_t count)
{
    int w_len=write(fd,buf,count);
    if(w_len==-1)
        {
            printf("write file error:%s",strerror(errno));
            return -1;
        }
    return w_len;
}
int Lseek(int fd,off_t offset,int whence)
{
    int len=lseek(fd,offset,whence);
    if(len==-1)
    {
        printf("Lseek file error:%s",strerror(errno));
        return -1;
    }
    return len;
}
ssize_t Read(int fd,void*buf,size_t count)
{
    ssize_t s=read(fd,buf,count);
    if(s==-1)
    {
        printf("readfile error:%s",strerror(errno));
        return -1;
    }
    return s;
}

int main(int argc, char *argv[]) {
    int fd1 = open("a.txt", O_RDONLY);
    if (fd1 < 0) {
        perror("open a.txt");
        exit(1);
    }

    int fd2 = open("b.txt", O_RDWR | O_CREAT, 0655);
    if (fd2 < 0) {
        perror("open b.txt");
        exit(1);
    }

    int file_size1 = lseek(fd1, 0, SEEK_END);
    lseek(fd1, 0, SEEK_SET);
    ftruncate(fd2, file_size1);

    void *addr1 = mmap(NULL, file_size1, PROT_READ, MAP_SHARED, fd1, 0);
    void *addr2 = mmap(NULL, file_size1, PROT_WRITE, MAP_SHARED, fd2, 0);


    memcpy(addr2, addr1, file_size1);

    munmap(addr1, file_size1);
    munmap(addr2, file_size1);

    close(fd1);
    close(fd2);

    printf("复制完成\n");

    return 0;
}
